package zwyarraylist;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;

import org.junit.Test;

import bean.UserBean;

/**
* 1、ArrayList中是可以有不通数据类型的数据的
* 2、
* @ClassName: ArrayList05Test 
* @Description: TODO
* @author zhaoweiyong  yyd 
* @date 2017年7月5日 上午9:32:35 *
 */
public class ArrayList05Test {

	/**
	 * 454行 public void add(int index, E element)
	 * 
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 * @title: testAdd
	 * @Author: zhaoweiyong
	 * @Date: 2017年7月4日下午5:18:50
	 */
	@Test
	public void testAdd() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ArrayList<Integer> dataList = new ArrayList<Integer>();
		dataList.add(1);

		Method addMethod = dataList.getClass().getDeclaredMethod("add", new Class[] { Object.class });
		Object result = addMethod.invoke(dataList, "前进一步！");
		//true
		System.out.println(result);
		System.out.println(dataList);
		//add(int index, E element)
		//rangeCheckForAdd 只能添加到索引0-size之内
		//扩容结构变化修改modCount
		//当前index之后的数据后移，System.arraycopy(elementData, index, elementData, index + 1, size - index);
		//elementData[index] = element
		//size ++;
		dataList.add(1, Integer.MAX_VALUE);
		System.out.println(dataList);
	}
	
	/**
	* 473行
	* @title: testRemove
	* @throws Exception 
	* @Author: zhaoweiyong
	* @Date: 2017年7月5日上午10:47:47
	 */
	@Test
	public void testRemoveIndex() throws Exception{
		ArrayList<Integer> dataList = new ArrayList<Integer>();
		dataList.add(1);

		Method addMethod = dataList.getClass().getDeclaredMethod("add", new Class[] { Object.class });
		Object result = addMethod.invoke(dataList, "前进一步！");
		//true
		System.out.println(result);
		System.out.println(dataList);
		//rangeCheck(index);
		//结构变化modCount++
		//size - index - 1
		//移动数据 System.arraycopy(elementData, index+1, elementData, index, numMoved);
		//lementData[--size] = null;
		//返回删除的数据 
		dataList.remove(0);
		System.out.println(dataList);
	}
	
	@Test
	public void testRemoveObject() throws Exception{
		ArrayList<Integer> dataList = new ArrayList<Integer>();
		dataList.add(1);

		Method addMethod = dataList.getClass().getDeclaredMethod("add", new Class[] { Object.class });
		Object result = addMethod.invoke(dataList, "前进一步！");
		//true
		System.out.println(result);
		System.out.println(dataList);
		//结构变化modCount++
		//size - index - 1
		//移动数据 System.arraycopy(elementData, index+1, elementData, index, numMoved);
		//lementData[--size] = null;
		//执行的是fastRemove
	    //Private remove method that skips bounds checking and does not return the value removed.
	    //private void fastRemove(int index)
		//正如所说的私有方法跳过范围检测 并且 不返回旧的数据
		dataList.remove("前进一步！");
		System.out.println(dataList);
	}
	
	/**
	* 535
	* @title: testClear
	* @throws Exception 
	* @Author: zhaoweiyong
	* @Date: 2017年7月5日上午11:23:31
	 */
	@Test
	public void testClear() throws Exception{
		ArrayList<Integer> dataList = new ArrayList<Integer>();
		dataList.add(1);
		//modCount变更
		//全部为Null
		//size = 0
		dataList.clear();
	}

	
	/**
	*
	* 535
	* @title: testToArray
	* @throws Exception 
	* @Author: zhaoweiyong
	* @Date: 2017年7月5日上午11:23:31
	 */
	@Test
	public void testToArray() throws Exception{
		/*ArrayList<UserBean> userBeanList = new ArrayList<UserBean>();
		UserBean userBean = new UserBean("lisy", 21);
		userBeanList.add(userBean);
		UserBean[] copyUserArr = new UserBean[1];
		copyUserArr = userBeanList.toArray(copyUserArr);
		UserBean copyUser = copyUserArr[0];
		//return Arrays.copyOf(elementData, size);
		//这样的作用其实是压缩
		System.out.println(copyUser == userBean);
		copyUser.setName("wangsy");
		System.out.println(userBean.getName());*/
		
		/*String[] userNameArr = new String[]{"春", "夏", "秋", "冬"};
		change(userNameArr);
		print(userNameArr);
		String[] userNameArrNew = Arrays.copyOf(userNameArr, 4);
		System.out.println(userNameArr == userNameArrNew);
		userNameArrNew[0] = "春";
		print(userNameArrNew);
		System.out.println();
		print(userNameArr);*/
		
		UserBean[] beanArr = new UserBean[4];
		beanArr[0] = new UserBean("A", 21);
		UserBean[] newBeanArr = Arrays.copyOf(beanArr, 4);
//		newBeanArr[0] = new UserBean("B", 22);
		newBeanArr[0].setName("B");
		System.out.println(newBeanArr[0]);
		System.out.println(beanArr[0]);
	}

	private void print(String[] paramArr) {
		for(String curStr : paramArr){
			System.out.print(curStr + " ");
		}
	}

	private void change(String[] userNameArr) {
		userNameArr[0] = "春雨";
	}
}
